package com.demo.zookeeper;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class ZKNodeAcl implements Watcher{

    private ZooKeeper zooKeeper = null;

    public static final String zkServerPath = "127.0.0.1:12181";
    public static final Integer timeout = 5000;
    private static Stat stat = new Stat();

    public ZKNodeAcl() {}

    public ZKNodeAcl(String connectString) {
        try {
            zooKeeper = new ZooKeeper(connectString, timeout, new ZKNodeAcl());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void createZKNode(String path, byte[] data, List<ACL> acls) {
        String result = "";
        try {
            // 同步创建节点
            result = zooKeeper.create(path, data, acls, CreateMode.PERSISTENT);
            System.out.println("创建节点: \t" + result + "\t成功...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws KeeperException, InterruptedException, NoSuchAlgorithmException {
        ZKNodeAcl zkServer = new ZKNodeAcl(zkServerPath);

        // acl 任何人都可以访问
//        zkServer.createZKNode("/aclimooc", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE);

        // 自定义用户认证访问
    //        List<ACL> acls = new ArrayList<>();
    //        Id imooc1 = new Id("digest", AclUtils.getDigestUserPwd("imooc1:123456"));
    //        Id imooc2 = new Id("digest", AclUtils.getDigestUserPwd("imooc2:123456"));
    //        acls.add(new ACL(ZooDefs.Perms.ALL, imooc1));
    //        acls.add(new ACL(ZooDefs.Perms.READ, imooc2));
    //        acls.add(new ACL(ZooDefs.Perms.DELETE | ZooDefs.Perms.CREATE, imooc2));
    //        zkServer.createZKNode("/aclimooc/testdigest", "testdigest".getBytes(), acls);

        // 注册过的用户必须通过addAuthInfo才能操作节点，参考命令行addauth
//        zkServer.getZooKeeper().addAuthInfo("digest", "imooc1:123456".getBytes());
//        zkServer.createZKNode("/aclimooc/testdigest/childtest", "childtest".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL);
//        Stat stat = new Stat();
//        byte[] data = zkServer.getZooKeeper().getData("/aclimooc/testdigest", false, stat);
//        System.out.println(new String(data));
//        zkServer.getZooKeeper().setData("/aclimooc/testdigest", "now".getBytes(), 1);

        // ip方式的acl
//        List<ACL> aclsIP = new ArrayList<>();
//        Id ipId1 = new Id("ip", "192.168.1.106");
//        aclsIP.add(new ACL(ZooDefs.Perms.ALL, ipId1));
//        zkServer.createZKNode("/aclimooc/iptest6", "iptest".getBytes(), aclsIP);
        zkServer.getZooKeeper().setData("/aclimooc/iptest6", "123".getBytes(), 0);
    }

    public ZooKeeper getZooKeeper() {
        return zooKeeper;
    }

    public void setZooKeeper(ZooKeeper zooKeeper) {
        this.zooKeeper = zooKeeper;
    }

    @Override
    public void process(WatchedEvent event) {

    }
}
